<?php

namespace app\home\controller\business;

use app\common\controller\Home;
/**
 * 客户中心控制器
 */
class Business extends Home
{
    // 构造函数，继承父类
    public function __construct()
    {
        parent::__construct();

        // 全局模型加载
        $this->BusinessModel = model('Business.Business');
        $this->RegionModel = model('Region');
    }

    // 客户中心
    public function index()
    {
        // 渲染模板
        return $this->view->fetch();
    }

    // 个人资料
    public function profile()
    {
        // 判断表单是否有提交
        if ($this->request->isPost()) {
            // 获取所有请求数据
            $nickname = $this->request->param('nickname', '', 'trim');
            $password = $this->request->param('password', '', 'trim');
            $gender = $this->request->param('gender', '', 'trim');
            $email = $this->request->param('email', '', 'trim');
            $region = $this->request->param('region', '', 'trim');

            // 组装数据
            $data = [
                'id'=>$this->LoginAuth['id'],
                'nickname'=>$nickname,
                'gender'=>$gender,
                'email'=>$email,
            ];

            // 判断密码是否为空，如果不为空就修改密码
            if(!empty($password))
            {
                // 判断密码是否就等于当前密码
                $newsalt = $this->LoginAuth['salt'];
                $repass = md5($password.$newsalt);

                if($repass == $this->LoginAuth['password'])
                {
                    $this->error('新密码不能和当前密码相同');
                    exit;
                }

                // 重新生成密码盐
                $salt = build_ranstr();

                $data['salt'] = $salt;
                $data['password'] = md5($password.$salt);
            }

            // 判断地区是否为空
            if(!empty($region))
            {
                // 字符串转换为数组
                // 转换为数组，并获取到最后一个元素
                $RegionList = explode('/', $region);
                $last = array_pop($RegionList);

                // 找出对应的地区码
                $path = $this->RegionModel->where(['name' => $last])->value('parentpath');

                // 字符串转换为数组
                $RegionCode = explode(',',$path);

                $data['province'] = isset($RegionCode[0]) ? $RegionCode[0] : '';
                $data['city'] = isset($RegionCode[1]) ? $RegionCode[1] : '';
                $data['district'] = isset($RegionCode[2]) ? $RegionCode[2] : '';
            }

            // 接收文件上传
            if(isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0)
            {
                // 调用公共方法
                $success = build_upload('avatar');

                if($success['result'])
                {
                    // 上传成功
                    $data['avatar'] = $success['data'];
                }else
                {
                    // 上传失败
                    $this->error($success['msg']);
                    exit;
                }
            }

            // 准备修改数据
            $result = $this->BusinessModel->isUpdate(true)->save($data);

            if($result === false)
            {
                $this->error($this->BusinessModel->getError());
                exit;
            }else
            {
                // 删除旧图片，更新cookie
                if(isset($data['avatar']))
                {
                    // 判断旧图片是否存在，如果存在就删除掉
                    @is_file(".".$this->LoginAuth['avatar']) && unlink(".".$this->LoginAuth['avatar']);

                    $login = [
                        'id' => $this->LoginAuth['id'],
                        'mobile' => $this->LoginAuth['mobile'],
                        'nickname' => $data['nickname'],
                        'avatar' => $data['avatar']
                    ];
                    // 更新cookie
                    cookie('LoginAuth',$login);
                }
                $this->success('修改成功',url('/home/business/business/index'));
                exit;
            }
        }

        return $this->view->fetch();
    }
}
